package com.qf.commons.core.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * author Ken
 * create_time 2022/11/15
 */
@Component
public class ApplicationUtils implements ApplicationContextAware {

    //静态IOC容器的引用
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        //Spring会调用该方法，将相关的对象设置进来
        ApplicationUtils.applicationContext = applicationContext;
    }

    /**
     * 根据类型从IOC容器中获取Bean对象
     * @param beanCls
     * @return
     * @param <T>
     */
    public static <T> T getBean(Class<T> beanCls) {
       return ApplicationUtils.applicationContext.getBean(beanCls);
    }
}
